<?php
namespace app\common\model;
use think\Model;
use sms\Aliyun\SignatureHelper;
use sms\Qcloud\SmsSingleSender;
class Sms extends Model{
	public function send_msg($type,$type_flag,$to){
		$array['mobile'] = $to;
		$array['type'] = $type;
        $array['code'] = trim(rand_string(6,1));
        if(!in_array($array['type'],['reg','bind','forgetpwd']) || empty($array['mobile'])  || empty($array['type'])){
            return ['code'=>-1,'msg'=>'参数错误'];
        }else{
			$array['type']='sms_tpl_'.$array['type'];
		}
		$cachename=md5(is_login().'_'.$to.'_'.$type);
		if(cache($cachename)){
		    return ['code'=>-5,'msg'=>'请不要频繁发送'];
		}
		$send=$GLOBALS['config']['user']['sms']['sms_type'];
		$result=$this->$send($array);
		if($result['code']==1){
			cache($cachename,$array['code'],300);
			return $result;
		}else{
			$this->error = $result['msg'];
			return false;
		}
		//cache($cachename,$array['code'],1*60);
		//return ['code'=>1,'msg'=>'发送成功','data'=>$array['code']];
	}	
    public function Aliyun($array){
        if(empty($array['mobile']) || empty($array['code']) || empty($array['type'])){
            return ['code'=>-1,'msg'=>'参数错误'];
        }
        $appid = $GLOBALS['config']['user']['sms']['sms_appid'];
        $appkey = $GLOBALS['config']['user']['sms']['sms_appkey'];
        $sign = $GLOBALS['config']['user']['sms']['sms_sign'];
        $security = false;
        $tpl = $GLOBALS['config']['user']['sms'][$array['type']];
        $params=[];
        $params['PhoneNumbers'] = $array['mobile'];
        $params['SignName'] = $sign;
        $params['TemplateCode'] = $tpl;
        $params['TemplateParam'] = [
            'code'=>$array['code'],
        ];
        if( is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }
        try {
            $helper = new SignatureHelper();
            $rsp = $helper->request(
                $appid,
                $appkey,
                "dysmsapi.aliyuncs.com",
                array_merge($params, array(
                    "RegionId" => "cn-hangzhou",
                    "Action" => "SendSms",
                    "Version" => "2017-05-25",
                )),
                $security
            );

            if($rsp['Code'] == 'OK'){
                $rsp['result'] = 1;
            }

            if($rsp['result'] ==1){
                return ['code'=>1,'msg'=>'发送成功'];
            }
            return ['code'=>-1,'msg'=>$rsp['Message']];
        }
        catch(\Exception $e) {
            return ['code'=>-2,'msg'=>'发生异常请重试'];
        }
    }
    public function Qcloud($array){
        if(empty($array['mobile']) || empty($array['code']) || empty($array['type'])){
            return ['code'=>-1,'msg'=>'参数错误'];
        }
        $appid = $GLOBALS['config']['user']['sms']['sms_appid'];
        $appkey = $GLOBALS['config']['user']['sms']['sms_appkey'];
        $sign = $GLOBALS['config']['user']['sms']['sms_sign'];
        $tpl = $GLOBALS['config']['user']['sms'][$array['type']];
        $params = [
            $array['code']
        ];

        try {
            $ssender = new SmsSingleSender($appid, $appkey);
            $result = $ssender->sendWithParam("86",$array['mobile'], $tpl, $params, $sign, "", "");

            $rsp = json_decode($result,true);
            if($rsp['result'] ==0){
                return ['code'=>1,'msg'=>'发送成功'];
            }
            return ['code'=>-1,'msg'=>$rsp['errmsg']];
        }
        catch(\Exception $e) {
            return ['code'=>-2,'msg'=>'发生异常请重试'];
        }
    }	
}
